<?php
/**
 * Created by xuyajun.
 * User: xuyajun
 * Date: 2019/10/30
 * Time: 15:55
 */
namespace App\Models\Account;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;

use Illuminate\Support\Facades\Request;
use DB;
use \Illuminate\Support\Facades\Input;
use App\ModelStatistic\StatisticCommon;

class LoginWhite extends Model
{
    protected $table='login_white';
    public $timestamps = false;


    public function paginate()
    {
        
        $perPage = Request::get('per_page', 20);
        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        
        $rw = \DB::connection($this->selectdb())->table($this->table);
        self::SourceCondition($rw, $cp_area);
        $rw->orderBy('id', "DESC");
        $total = $rw->get()->count();
        $rw = $rw->skip($start)->take($perPage);
        $data = $rw->get();
        $data = $data->toArray();

        $list = static::hydrate($data);

        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());
//var_dump($paginator);exit();
        return $paginator;
    }

    public function findByAccount($account) 
    {
        $data = \DB::connection($this->selectdb())->table($this->table)->where('account', $account)->first();
        return $data;
    }

    public function selectdb($select = ''){
        return 'mysql_bgac';
    }
    public function getConnectionName(){
        return $this->selectdb();
    }

    public static function stateOptions()
    {
        return [0 => 'Off', 1 => 'On'];
    }

    public static function SourceCondition(&$rw, $area)
    {
        $cond = Request::all();

        if ($cond['account']){
            $rw->where("account", 'like', $cond["account"].'%');
        }

        if (isset($cond['state'])) {
            $rw->where('state', $cond['state']?:0);
        }
    }
}